﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Admin_AdminProductIphone : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        command = new SqlCommand();
        if(!IsPostBack){
            bindDataToGridView();
        }
    }
    protected void bindDataToGridView()
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spGetProductIphone", connect);
            command.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(command);
            da.Fill(ds);
            gridViewIphoneManager.DataSource = ds.Tables[0];
            gridViewIphoneManager.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }
    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminProductsIphoneAddNew.aspx");
    }
    protected void getIphone(string ID){
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spGetProductIphoneByID", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@ID", SqlDbType.NVarChar).Value = ID;
            command.ExecuteNonQuery();
            da = new SqlDataAdapter(command);
            da.Fill(ds);
            string img1 = ds.Tables[0].Rows[0][4].ToString();
            string name1 = ds.Tables[0].Rows[0][1].ToString();
            string type1 = ds.Tables[0].Rows[0][2].ToString();
            string price1 = ds.Tables[0].Rows[0][3].ToString();
            string id1 = ds.Tables[0].Rows[0][0].ToString();
            Response.Redirect("AdminProductIphoneUpdate.aspx?Image=" + img1 + "&ID=" + id1 + "&Name=" + name1 + "&Type=" + type1+"&Price="+price1);
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }
    protected void gridViewIphoneManager_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gridViewIphoneManager.SelectedRow;
        //lblStatus.Text = "Ban chon " + row.Cells[1].Text + ".";
        getIphone(row.Cells[1].Text);
    }
}